package io.zerax.moyu_chat;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import io.zerax.moyu_chat.R;
import android.widget.LinearLayout;
import android.view.Gravity;

public class MessageAdapter extends ArrayAdapter<Message>
{
    public MessageAdapter(Context context) 
    {
        super(context, R.layout.message);
    }
    
    public MessageAdapter(Context context, Message[] values) 
    {
        super(context, R.layout.message, values);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.message, parent, false);

        Message msg = getItem(position);
        
        LinearLayout layout = view.findViewById(R.id.msg_box);

        TextView name = view.findViewById(R.id.msg_name);
        name.setText(msg.name);
        

        TextView content = view.findViewById(R.id.msg_content);
        content.setText(msg.content);
        
        if (!msg.isUser) {
            name.setTextColor(0xFFE6B340);
            layout.setGravity(Gravity.LEFT);
        } else {
            name.setTextColor(0XFF334CB2);
            layout.setGravity(Gravity.RIGHT);
        }
        
        return view;
    }
}
